<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            background-color: pink;
        }
    </style>
</head>

<body>
    <div id="test"></div>

    <!-- 引入核心库 -->
    <script type="text/javascript" src="./React-js/16.8/react.development.js"></script>
    <!-- 用于支持react操作DOM -->
    <script text="text/javascript" src="./React-js/16.8/react-dom.development.js"></script>
    <!-- 用于将jsx转换为js -->
    <script text="text/javascript" src="./React-js/16.8/babel.min.js"></script>

    <script type="text/babel">
        class Life extends React.Component {

            state = {opacity:1}
            death = ()=>{
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }

            // 组件挂载完
            componentDidMount(){
                this.timer = setInterval(()=>{
                    let {opacity} = this.state
                    opacity -= 0.1
                    if(opacity <=0) opacity = 1
                   this.setState({opacity})
                },100)
            }

            // 组件卸载前
            componentWillUnmount(){
                clearInterval(this.timer)
            }

            render() {
                return (
                    <div>
                        <h2 style={{opacity:this.state.opacity}}>现在的文字</h2>
                        <button onClick={this.death}>点击</button>
                    </div>
                )
            }
        }


        ReactDOM.render(<Life />, document.querySelector('#test'))
    </script>
</body>

</html>